package com.china08.yxyapi.repository.mg.homework;

import java.util.Date;
import java.util.List;

import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.mongodb.repository.Query;

import com.china08.yxyapi.entity.mg.homework.HomeworkTaskClass;

/**
 * 作业系统:作业:知识点:对应表, 每一个班任务 存储一条记录
 * 
 * @author Richard Wang
 *
 */
public interface HomeworkTaskClassRepository extends MongoRepository<HomeworkTaskClass, String> {

    HomeworkTaskClass findTop1ByPublishDateLessThanEqualAndPublishUserIdAndSchoolIdAndTitleLikeAndClassIdAndStageOrderByPublishDateDesc(
            Date dateTime, String userId, String schoolId, String keyword, String classId, String stage);

    HomeworkTaskClass findTop1ByPublishDateLessThanEqualAndPublishUserIdAndSchoolIdAndTitleLikeAndClassIdAndSubjectAndStageOrderByPublishDateDesc(
            Date dateTime, String userId, String schoolId, String keyword, String classId, String subjectId,
            String stage);

    HomeworkTaskClass findTop1ByPublishDateLessThanEqualAndPublishUserIdAndSchoolIdAndTitleLikeAndStageOrderByPublishDateDesc(
            Date dateTime, String userId, String schoolId, String keyword, String stage);

    HomeworkTaskClass findTop1ByPublishDateLessThanEqualAndPublishUserIdAndSchoolIdAndTitleLikeAndSubjectAndStageOrderByPublishDateDesc(
            Date dateTime, String userId, String schoolId, String keyword, String subjectId, String stage);

    HomeworkTaskClass findTop1ByPublishDateLessThanEqualAndPublishUserIdAndSchoolIdAndStageOrderByPublishDateDesc(
            Date dateTime, String userId, String schoolId, String stage);

    HomeworkTaskClass findTop1ByPublishDateLessThanEqualAndPublishUserIdAndSchoolIdAndSubjectAndStageOrderByPublishDateDesc(
            Date dateTime, String userId, String schoolId, String subjectId, String stage);

    HomeworkTaskClass findTop1ByPublishDateLessThanEqualAndPublishUserIdAndSchoolIdAndClassIdAndStageOrderByPublishDateDesc(
            Date dateTime, String userId, String schoolId, String classId, String stage);

    HomeworkTaskClass findTop1ByPublishDateLessThanEqualAndPublishUserIdAndSchoolIdAndClassIdAndSubjectAndStageOrderByPublishDateDesc(
            Date dateTime, String userId, String schoolId, String classId, String subjectId, String stage);

    List<HomeworkTaskClass> findByPublishDateBetweenAndPublishUserIdAndSchoolIdAndTitleLikeAndClassIdAndStageOrderByPublishDateDesc(
            Date dateStart, Date dateEnd, String userId, String schoolId, String keyword, String classId, String stage);

    List<HomeworkTaskClass> findByPublishDateBetweenAndPublishUserIdAndSchoolIdAndTitleLikeAndClassIdAndSubjectAndStageOrderByPublishDateDesc(
            Date dateStart, Date dateEnd, String userId, String schoolId, String keyword, String classId,
            String subjectId, String stage);

    List<HomeworkTaskClass> findByPublishDateBetweenAndPublishUserIdAndSchoolIdAndTitleLikeAndStageOrderByPublishDateDesc(
            Date dateStart, Date dateEnd, String userId, String schoolId, String keyword, String stage);

    List<HomeworkTaskClass> findByPublishDateBetweenAndPublishUserIdAndSchoolIdAndTitleLikeAndSubjectAndStageOrderByPublishDateDesc(
            Date dateStart, Date dateEnd, String userId, String schoolId, String keyword, String subjectId,
            String stage);

    List<HomeworkTaskClass> findByPublishDateBetweenAndPublishUserIdAndSchoolIdAndClassIdAndStageOrderByPublishDateDesc(
            Date dateStart, Date dateEnd, String userId, String schoolId, String classId, String stage);

    List<HomeworkTaskClass> findByPublishDateBetweenAndPublishUserIdAndSchoolIdAndClassIdAndSubjectAndStageOrderByPublishDateDesc(
            Date dateStart, Date dateEnd, String userId, String schoolId, String classId, String subjectId,
            String stage);

    List<HomeworkTaskClass> findByPublishDateBetweenAndPublishUserIdAndSchoolIdAndStageOrderByPublishDateDesc(
            Date dateStart, Date dateEnd, String userId, String schoolId, String stage);

    List<HomeworkTaskClass> findByPublishDateBetweenAndPublishUserIdAndSchoolIdAndSubjectAndStageOrderByPublishDateDesc(
            Date dateStart, Date dateEnd, String userId, String schoolId, String subjectId, String stage);

    HomeworkTaskClass findTop1ByTaskIdAndClassId(String taskId, String classId);

    @Query(value = "{'predict_date' : {$gte: ?0, $lte : ?1} }")
    List<HomeworkTaskClass> findByPredictDateBetweenEqual(Date beginDate, Date endDate);

    /**
     * 查询答题截止日期之后多长时间的记录
     * 
     * @param predictDate
     *            截取时间向后拖延多长时间
     * @return 查询答题截止日期之后多长时间的记录
     */
    List<HomeworkTaskClass> findByPredictDateGreaterThan(Date predictDate);

    void deleteByTaskId(String taskId);

    void deleteByTaskIdAndClassId(String taskId, String classId);

    @Query(value = "{'$where': 'this.submit_of_student > this.marking_of_student', 'school_id': ?1, 'publish_user_id': ?0, 'task_id': {$in: ?2}}", count = true)
    Integer countByWorkTotal(String userId, String schoolId, List<String> taskIds);

    List<HomeworkTaskClass> findBySourceNotAndTaskType(int i, String string);

    List<HomeworkTaskClass> findByTaskIdIn(List<String> ids);

    // List<HomeworkTaskClass> findByPredictDateLessThan(int i, Date
    // currentDate);

}
